package com.store.app.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.store.app.R;
import com.store.app.bean.PayBean;
import com.store.app.utils.PayUtil;
import com.store.app.utils.SettingData;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("zyl","WXPayEntryActivity onCreate");
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, SettingData.APP_ID);
        api.handleIntent(getIntent(), this);
    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        

    }

    @Override
    public void onResp(BaseResp resp) {
        Log.v("zyl","onResp resp.errCode:"+resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            if(resp.errCode == 0)
            {
                //发送广播关闭当前页面
                Intent intent = new Intent();
                intent.setAction("FINISH");
                this.sendBroadcast(intent);
                EventBus.getDefault().post(new PayBean(2));
//                if(PayUtil.tid!=null&&PayUtil.payment_id!=null) {
//                    //通知js
//                    PayUtil.goBackToHYD("1", PayUtil.tid, PayUtil.payment_id);
//                }
//                Toast.makeText(WXPayEntryActivity.this, "支付成功,订单已生成", Toast.LENGTH_LONG).show();
            }else if(resp.errCode == -2)
            {
                EventBus.getDefault().post(new PayBean(-2));
                Toast.makeText(WXPayEntryActivity.this, "支付取消", Toast.LENGTH_LONG).show();
            }else if(resp.errCode == -1)
            {
                EventBus.getDefault().post(new PayBean(-1));
                Toast.makeText(WXPayEntryActivity.this, "支付失败", Toast.LENGTH_LONG).show();
            }
            finish();
        }
    }
}